package com.ruoyi.hospital.service.impl;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.department.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hospital.mapper.HospitalMapper;
import com.ruoyi.hospital.domain.Hospital;
import com.ruoyi.hospital.service.IHospitalService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【医院】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-03
 */
@Service
public class HospitalServiceImpl implements IHospitalService 
{
    @Autowired
    private HospitalMapper hospitalMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Hospital selectHospitalById(Long id)
    {
        return hospitalMapper.selectHospitalById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param hospital 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Hospital> selectHospitalList(Hospital hospital)
    {
        return hospitalMapper.selectHospitalList(hospital);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param hospital 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertHospital(Hospital hospital)
    {
        return hospitalMapper.insertHospital(hospital);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param hospital 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateHospital(Hospital hospital)
    {
        return hospitalMapper.updateHospital(hospital);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHospitalByIds(Long[] ids)
    {
        return hospitalMapper.deleteHospitalByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHospitalById(Long id)
    {
        return hospitalMapper.deleteHospitalById(id);
    }

    @Override
    public AjaxResult addFile(MultipartFile file) throws IOException {
        ExcelUtil<Hospital> util = new ExcelUtil<>(Hospital.class);
        List<Hospital> hospitals = util.importExcel(file.getInputStream());

        // 1. 去除 hospitalName 的前后空格
        hospitals.forEach(h -> {
            if (h.getHospitalName() != null) {
                h.setHospitalName(h.getHospitalName().trim());
            }
        });

        // 2. Excel 内部去重（按 hospitalName，保留第一条）
        Map<String, Hospital> uniqueExcelMap = hospitals.stream()
                .filter(h -> StringUtils.isNotBlank(h.getHospitalName()))
                .collect(Collectors.toMap(
                        Hospital::getHospitalName,
                        h -> h,
                        (existing, replacement) -> existing
                ));
        List<Hospital> uniqueExcelList = new ArrayList<>(uniqueExcelMap.values());

        // 3. 查询数据库已有的医院列表，并去除名称前后空格
        List<Hospital> hospitalsInDb = hospitalMapper.selectHospitalList(new Hospital());
        Set<String> existingHospitalNames = hospitalsInDb.stream()
                .map(Hospital::getHospitalName)
                .filter(StringUtils::isNotBlank)
                .map(String::trim)
                .collect(Collectors.toSet());

        // 4. 过滤掉数据库中已存在的医院
        List<Hospital> newHospitals = uniqueExcelList.stream()
                .filter(h -> !existingHospitalNames.contains(h.getHospitalName()))
                .collect(Collectors.toList());

        // 5. 插入新医院
        for (Hospital h : newHospitals) {
            hospitalMapper.insertHospital(h);
        }

        return AjaxResult.success("导入成功，共导入 " + newHospitals.size() + " 条新数据");
    }

}
